﻿using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Text;

namespace SIMcSDK
{
    public delegate void GetConfigData(int cfgId);

    public class ConfigControl
    {
        #region DLL declarations

        [DllImport("pjsipDll.dll")]
        private static extern StringBuilder dll_getCodec(int index);
        [DllImport("pjsipDll.dll")]
        private static extern int dll_getNumOfCodecs();
        [DllImport("pjsipDll.dll")]
        private static extern int dll_setCodecPriority(String name, int prio);
        [DllImport("pjsipDll.dll")]
        private static extern void dll_setSipConfig(SIMcConfigStruct config);

        #endregion DLL declarations

        /*
        #region Callback declarations              
        // Callback function registration declarations 
        // passing delegate to unmanaged code (.dll)        
        [DllImport("pjsipDll.dll", EntryPoint = "getConfigDataCallback")]
        private static extern int getConfigDataCallback(GetConfigData cb);

        // Static declaration because of CallbackonCollectedDelegate exception!
        static GetConfigData gcdDel = new GetConfigData(getConfigData);

        #endregion Callback declarations

        #region Callbacks
        private static void getConfigData(int cfgId)
        {
            return;
        }

        #endregion Callbacks

        */

        #region Methods
        /// <summary>
        /// Static initializer. Call this method to set callbacks from SIP stack. 
        /// </summary>
        public static void initialize()
        {
            // assign callbacks
            //getConfigDataCallback(gcdDel);
        }

        public static string getCodec(int index)
        {
            StringBuilder buf = dll_getCodec(index);
            return buf.ToString();
        }

        public static int getNoOfCodecs()
        {
            int no = dll_getNumOfCodecs();
            return no;
        }

        public static void setCodecPriority(string codecname, int priority)
        {
            //if (!IsInitialized) return;

            dll_setCodecPriority(codecname, priority);
        }

        public static void setConfig(SIMcConfigStruct config)
        {
            //if (!IsInitialized) return;

            dll_setSipConfig(config);
        }
        #endregion Methods

        [StructLayout(LayoutKind.Sequential)]
        public class SIMcConfigStruct
        {
            public int listenPort = 5060;
            public int expires = 3600;

            [MarshalAs(UnmanagedType.I1)]
            public bool VADEnabled = true;

            public int ECTail = 200;
            public int clock_rate = 8000;
            public int quality = 1;
            public int ptime = 200;

            [MarshalAs(UnmanagedType.I1)]
            public bool iceEnabled = true;

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
            public string pjsiplogpath;

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
            public string stun;

            [MarshalAs(UnmanagedType.I1)]
            public bool useStun = false;
        }
    }
}
